package licode.unisop.client.feign.impl;

import licode.unisop.client.api.auth.SopUserAuth;
import licode.unisop.client.feign.SoUserVisitFeign;
import licode.unisop.client.info.*;
import licode.unisop.client.utils.SopRequestParse;
import licode.unisop.client.utils.StringUtil;
import licode.unisop.client.visit.UniClientTool;
import licode.unisop.client.visit.UserVisit;
import licode.unisop.client.vo.SopAuthPlatform;
import licode.unisop.client.vo.SopUser;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户操作服务类
 *
 * @author licode
 */
@Component
public class UserVisitFeignImpl implements UserVisit {
    @Resource
    private HttpServletRequest request;

    @Resource
    private UniClientTool uniClientTool;

    @Resource
    private SopUserAuth sopUserAuth;

    @Resource
    private SoUserVisitFeign soUserVisitFeign;

    @Override
    public void updateCache(SopUserIdIn userId) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, userId.getPlatformId());
        soUserVisitFeign.updateCache(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), userId);
    }

    public String updatePassword(SopUpdatePasswordIn info) {
        SopTokenInfo tokenInfo;
        tokenInfo = SopRequestParse.getAccessTokenInfo(request);
        info.setAccessToken(tokenInfo.getToken());
        info.setRealmId(tokenInfo.getRealmId());
        info.setPlatformId(tokenInfo.getPlatformId());
        return sopUserAuth.updatePassword(info);
    }

    @Override
    public void resetPassword(SopResetPasswordIn info) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, info.getPlatformId());
        soUserVisitFeign.resetPassword(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), info);
    }

    public SopUser save(SopUser userIn) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, userIn.getPlatformId());
        if (StringUtil.isBlank(userIn.getRealmId())) {
            userIn.setRealmId(authPlatform.getRealmId());
        }
        userIn.setGroupId("default");

        return soUserVisitFeign.saveUser(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), userIn).getData();
    }

    public Boolean saveAccount(SopAccountIn accountIn) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, accountIn.getPlatformId());
        accountIn.setRealmId(authPlatform.getRealmId());
        soUserVisitFeign.saveAccount(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), accountIn);
        return true;
    }

    public Boolean saveIdentity(SopIdentityIn identityIn) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, identityIn.getPlatformId());
        identityIn.setRealmId(authPlatform.getRealmId());
        soUserVisitFeign.saveIdentity(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), identityIn);
        return true;
    }
}
